package sailhero

import (
	"github.com/wsw188440873/ctrl-cali/types"
	"math"
)

func rangeNoiseCalculate(modelId string, params map[string]interface{}) (float64, bool) {
	if _, ok := params["collect_data"]; !ok {
		return 0, false
	}
	if _, ok := types.Range[modelId]; !ok {
		return 0, false
	}

	avgMap, ok := params["collect_data"].(map[string][]float64)
	if ok == false {
		return 0, false
	}

	var n int
	var sum float64
	for _, values := range avgMap {

		n += len(values)
		for _, v := range values {
			sum += v
		}
		avg := sum / float64(n)

		var riSum float64
		// 求和(ri- 平均值)e2
		for _, v := range values {
			riSum += math.Pow(v-avg, 2)
		}
		return math.Sqrt(riSum / (float64(n) - 1)), true
	}

	return 0, false
}
